var utils = require("../utils.js");
var watch_video = require("./watch_video.js");
var read_article_storage = storages.create("read_article");

function get_read_article_times() {
  return read_article_storage.get("times") || 0;
}
function set_read_article_times(times) {
  read_article_storage.put("times", times);
}

var read_article = {
  seconds: 0.5 * 60,
  init() {
    sleep(5 * 1000);
    if (utils.click_text("推荐")) {
      sleep(2 * 1000);
      this.execute(this.seconds);
    } else {
      console.log("read_article init", "没找到【推荐】");
      this.end();
    }
  },
  execute(seconds) {
    var start_time = Date.now();
    while (true) {
      swipe(
        device.width / 2,
        device.height / 2,
        device.width / 2,
        device.height / 2 - 800,
        1000
      );
      var interval = random(1, 5);
      sleep(interval * 1000);
      var end_time = Date.now();
      console.log(
        "read_article execute:",
        "文章已看",
        ((end_time - start_time) / 1000).toFixed(2),
        "s",
        ((end_time - start_time) / 1000 / 60).toFixed(2),
        "m"
      );
      if (end_time - start_time > seconds * 1000) {
        break;
      }
    }
    this.after();
  },
  after() {
    this.end();
  },
  end() {
    sleep(2 * 1000);
    utils.click_text("百度");
    console.log("read_article end:");
    this.next();
  },
  next() {
    console.log("read_article next:");
    watch_video.init();
  },
};

module.exports = read_article;
